package gui;

import java.awt.event.*;

import gameBasicElements.PlayerColor;
import gameEngine.GameInfo;

import javax.swing.*;

/**
 * @author Wojciech Łopata
 *
 */

public class PlayerInfoPanel extends JPanel
{
	private static final long serialVersionUID = 193177702896147432L;
	private final JLabel mainLabel;
	private final JLabel captivesLabel;
	private final JLabel timeLeftLabel;
	private final JTextField captivesField;
	private final JTextField timeLeftField;
	
	private final Timer timer;
	private final ActionListener timerActionListener;
	
	private final PlayerColor player;
	private GameInfo info;
	
	public PlayerInfoPanel(PlayerColor playerColor)
	{
		mainLabel = new JLabel("Gracz " + (playerColor.equals(PlayerColor.BLACK) ? "1 [czarne]:" : "2 [białe]:"));
		captivesLabel = new JLabel("Jeńcy:");
		timeLeftLabel = new JLabel("Pozostały czas:");
		captivesField = new JTextField();
		captivesField.setEditable(false);
		timeLeftField = new JTextField();
		timeLeftField.setEditable(false);
		
		player = playerColor;
		
		timerActionListener = new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				if(info != null)
				{
					int minutes = info.getTimeLeft(player) / 60000;
					int seconds = (info.getTimeLeft(player) % 60000) / 1000;
					timeLeftField.setText((minutes < 10 ? "0" : "") + minutes + ":" + (seconds < 10 ? "0" : "") + seconds);
					captivesField.setText(info.getCurrentBoard().captivesScore(player) + "");
				}
			}	
		};
		timer = new Timer(500, timerActionListener);
		initComponents();
	}
	
	public void startDrawing(GameInfo info)
	{
		this.info = info;
		if(timer.isRunning()) timer.stop();
		timer.start();
	}
	
	public void stopDrawing()
	{
		if(timer.isRunning()) timer.stop();
	}
	
    private void initComponents()
    {
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(mainLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 189, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(captivesLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 82, Short.MAX_VALUE)
                        .addComponent(captivesField, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(timeLeftLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 23, Short.MAX_VALUE)
                        .addComponent(timeLeftField, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(mainLabel)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(captivesLabel)
                    .addComponent(captivesField, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(timeLeftLabel)
                    .addComponent(timeLeftField, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
    }
}
